//package com.link.play.fit.ai.ui.view
//
//import android.content.Context
//import android.graphics.Color
//import android.util.AttributeSet
//import android.view.LayoutInflater
//import android.view.View
//import android.view.ViewGroup
//import android.widget.LinearLayout
//import android.widget.TextView
//import androidx.constraintlayout.widget.ConstraintLayout
//import androidx.recyclerview.widget.DiffUtil
//import androidx.recyclerview.widget.LinearLayoutManager
//import androidx.recyclerview.widget.ListAdapter
//import androidx.recyclerview.widget.PagerSnapHelper
//import androidx.recyclerview.widget.RecyclerView
//import androidx.recyclerview.widget.RecyclerView.HORIZONTAL
//import com.link.play.fit.ai.R
//import kotlin.math.min
//
//// 日期分页控件实现
//class DatePagerView(context: Context, attrs: AttributeSet) : ConstraintLayout(context, attrs) {
//
//    private val recyclerView: RecyclerView
//    private val adapter = DatePagerAdapter()
//    private var maxDays = 30 // 最大天数
//    private var currentDay = 1 // 当前天数
//
//    init {
//        inflate(context, R.layout.view_date_pager, this)
//        recyclerView = findViewById(R.id.recyclerView)
//
//        setupRecyclerView()
//    }
//
//    private fun setupRecyclerView() {
//        recyclerView.layoutManager = LinearLayoutManager(context, HORIZONTAL, false)
//        recyclerView.adapter = adapter
//        PagerSnapHelper().attachToRecyclerView(recyclerView)
//    }
//
//    fun updateDays(currentDay: Int, maxDays: Int) {
//        this.currentDay = currentDay.coerceAtMost(maxDays)
//        this.maxDays = maxDays
//
//        val pages = calculatePages()
//        adapter.submitList(pages)
//
//        // 滚动到当前页
//        recyclerView.post {
//            val targetPos = pages.indexOfFirst { it.contains(currentDay) }
//            if (targetPos != -1) {
//                recyclerView.scrollToPosition(targetPos)
//            }
//        }
//    }
//
//    private fun calculatePages(): List<IntRange> {
//        val pages = mutableListOf<IntRange>()
//        var pageStart = 1
//
//        while (pageStart <= maxDays) {
//            val pageEnd = min(pageStart + 6, maxDays)
//            pages.add(pageStart..pageEnd)
//            pageStart = pageEnd + 1
//        }
//
//        return pages.reversed() // 倒序排列，最新日期页在前
//    }
//
//    inner class DatePagerAdapter : ListAdapter<IntRange, DateViewHolder>(
//        DiffCallback
//    ) {
//        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DateViewHolder {
//            return DateViewHolder(
//                LayoutInflater.from(parent.context)
//                    .inflate(R.layout.item_date_page, parent, false))
//        }
//
//        override fun onBindViewHolder(holder: DateViewHolder, position: Int) {
//            val days = getItem(position)
//            holder.bind(days, currentDay)
//        }
//    }
//
//    class DateViewHolder(view: View) : RecyclerView.ViewHolder(view) {
//        private val container: LinearLayout = view.findViewById(R.id.container)
//
//        fun bind(days: IntRange, currentDay: Int) {
//            container.removeAllViews()
//            days.forEach { day ->
//                val tv = TextView(itemView.context).apply {
//                    text = "第${day}天"
//                    setTextColor(if (day == currentDay) Color.RED else Color.BLACK)
//                    setPadding(5.dpToPx(), 8.dpToPx(), 5.dpToPx(), 8.dpToPx())
//                }
//                container.addView(tv)
//            }
//        }
//
//        private fun Int.dpToPx(): Int = (this * itemView.context.resources.displayMetrics.density).toInt()
//    }
//
//    object DiffCallback : DiffUtil.ItemCallback<IntRange>() {
//        override fun areItemsTheSame(oldItem: IntRange, newItem: IntRange): Boolean {
//            return oldItem.first == newItem.first
//        }
//
//        override fun areContentsTheSame(oldItem: IntRange, newItem: IntRange): Boolean {
//            return oldItem == newItem
//        }
//    }
//}